//MINI UI解析
mini.parse();

/**
 * 订单信息处理器类
 */
function OrderHandler() {

}

//实例化全局订单处理器
OrderHandler.instance = new OrderHandler();
OrderHandler.mgrData = new OrderMgrData();
OrderHandler.orderQueryForm = new mini.Form("orderQueryForm");
OrderHandler.isQueryBtnExpended = false;

OrderHandler.orderGrid = mini.get("orderGrid");
OrderHandler.orderGrid.url = OrderHandler.mgrData.loadAllOrderListUrl("orderList");
OrderHandler.orderPanel = mini.get("orderPanel");
OrderHandler.boiOrder = mini.get("boiOrder");
OrderHandler.boiPay = mini.get("boiPay");
OrderHandler.boiShip = mini.get("boiShip");

//定义订单列表页功能权限字符编码数组
OrderHandler.purArrayOList = new Array("order_list_quer", "order_list_qux", "order_list_yic", 
		"order_list_query", "order_list_adv_query", "order_list_refresh");
		
//定义订单列表页功能权限字符编码JSON		
OrderHandler.purJsonOrder ={
		orderDetail : "order_list_detail"
};

/**
 * 功能按钮权限控制及其展示
 */
OrderHandler.loadPurviewBtn = function() {
	var userArr = MiniuiTools.isHasPurview(OrderHandler.purArrayOList);
	
	for (var i = 0; i < userArr.length; i++) {
		var purviewBtn = mini.get(OrderHandler.purArrayOList[i]);
		
		if(purviewBtn){
			purviewBtn.setVisible(userArr[i]);
		}
	}
};

/**
 * 初始化数据
 */
OrderHandler.init = function(){
	OrderHandler.loadPurviewBtn();
	
	if(gbIsAdvQuery != null && gbIsAdvQuery){
		var orderBeginTime = (gbQueryData.orderBeginTime != null) ? (gbQueryData.orderBeginTime.replace("T"," ")) : "";
		var orderEndTime = (gbQueryData.orderEndTime != null) ? (gbQueryData.orderEndTime.replace("T"," ")) : ""; 
		gbQueryData.orderBeginTime = orderBeginTime;
		gbQueryData.orderEndTime = orderEndTime;
		
		OrderHandler.orderGrid.load({orderQueryData: mini.encode(gbQueryData)});
	}else{
		OrderHandler.orderGrid.load();
	}
	
	
	OrderHandler.boiOrder.setData(OrderHandler.mgrData.boiOrderData);
	OrderHandler.boiOrder.setValue(OrderHandler.mgrData.boiOrderData[0].id);
	OrderHandler.boiPay.setData(OrderHandler.mgrData.boiPayData);
	OrderHandler.boiPay.setValue(OrderHandler.mgrData.boiPayData[0].id);
	OrderHandler.boiShip.setData(OrderHandler.mgrData.boiShipData);
	OrderHandler.boiShip.setValue(OrderHandler.mgrData.boiShipData[0].id);
};  

OrderHandler.init();

/**
 * 筛选不同条件下不符合订单操作的记录数
 * @param {} disposeType
 * @param {} rowsData
 * @return {}
 */
OrderHandler.statinValidCounts = function(disposeType, rowsData){
	var boiPay_0 = null;
	var count1 = 0;
	var count2 = 0;
	
	for(var i = 0; i < rowsData.length; i++){
		var boiOrder_i = rowsData[i].boiOrder;
		var boiPay_i = rowsData[i].boiPay;
		var boiShip_i = rowsData[i].boiShip;
		
		if(disposeType == OrderHandler.mgrData.queRBtn){
			if(!(boiOrder_i == 0 && boiPay_i == 0 && boiShip_i == 0)){
				count1 ++;
			}
		}else if(disposeType == OrderHandler.mgrData.quXBtn){
			if(!((boiOrder_i == 0 && boiPay_i == 0 && boiShip_i == 0)
				|| (boiOrder_i == 1 && boiPay_i == 0 && boiShip_i == 0)
					|| (boiOrder_i == 1 && boiPay_i == 1 && boiShip_i == 0)
						|| (boiOrder_i == 1 && boiPay_i == 1 && boiShip_i == 1))){
				count1 ++;
			}else{
				if(!boiPay_0){
					boiPay_0 = boiPay_i;
				}else{
					if(boiPay_0 != boiPay_i) count2 ++;
				}
			}
		}else if(disposeType == OrderHandler.mgrData.yiCBtn){
			if(!((boiOrder_i == 2 && boiPay_i == 0 && boiShip_i == 0)
					|| (boiOrder_i == 2 && boiPay_i == 2 && boiShip_i == 0)
						|| (boiOrder_i == 4 && boiPay_i == 2 && boiShip_i == 5)
							|| (boiOrder_i == 4 && boiPay_i == 2 && boiShip_i == 6))){
				count1 ++;
			}
		}
	}
	
	if(count1 > 0){
		if(disposeType == OrderHandler.mgrData.queRBtn){
			MiniuiTools.miniWarning('系统提示', '选择的资源中有' + count1 + '条记录不能执行确认操作，请重试！');
		}
		
		if(disposeType == OrderHandler.mgrData.quXBtn){
			MiniuiTools.miniWarning('系统提示', '选择的资源中有' + count1 + '条记录不能执行取消操作，请重试！');
		}
		
		if(disposeType == OrderHandler.mgrData.yiCBtn){
			MiniuiTools.miniWarning('系统提示', '选择的资源中有' + count1 + '条记录不能执行移除操作，请重试！');
		}
		
		return false;
	}
	
	if(disposeType == OrderHandler.mgrData.quXBtn && count2 > 0){
		MiniuiTools.miniWarning('系统提示', '选择的资源支付状态不一致，不能执行取消操作，请重试！');
		
		return false;
	}
	
	return true;
}

/**
 * 订单信息批量确认处理程序
 * @param {} rowsData
 */
OrderHandler.queRenOrders = function(rowsData){
	mini.confirm("是否确认选中的" + rowsData.length + "条订单资源？" ,"系统提示",
		function(action, value){
			if (action == "ok") {
				var ids = "";
				for ( var i = 0; i < rowsData.length - 1; i++){
					ids += rowsData[i].boiId + ",";
				}
				ids += rowsData[rowsData.length - 1].boiId;
				
				$.ajax({
			        url: OrderHandler.mgrData.queRenOrdersUrl(),
			        type: "post",
			        dataType : 'json',
			        data: {
			    		boiIds: ids, 
			    		operateUType: gbOperateUType,
			    		operateUser: gbOperateUser,
			    		operateIp: gbOperateIp
			        },
			        cache: false,
			        success: function (text) {
			        	OrderHandler.orderGrid.reload();
			        },
			        error: function (jqXHR, textStatus, errorThrown) {
			        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
			        },
					complete : function(){
						OrderHandler.orderGrid.unmask();
					}
			    });
			    
			    OrderHandler.orderGrid.loading();
			}
		});
}

/**
 * 展开批量取消订单信息窗口处理程序
 * @param {} rowsData
 * @param {} isPaid 是否已付款
 */
OrderHandler.showCancelWin = function(rowsData, isPaid){
	var boiIds = "";
	for ( var i = 0; i < rowsData.length - 1; i++){
		boiIds += rowsData[i].boiId + ",";
	}
	boiIds += rowsData[rowsData.length - 1].boiId;
				
	miniWin.afreshNewWin();
	miniWin.setParams(363, 315, "批量取消订单"); 
	miniWin.setOption("bodyStyle","padding:0px;");

	miniWin.load("cancelOrder.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {
        	boiId: boiIds, 
        	isPaid: isPaid,
        	selectNum: rowsData.length,
        	isBatch: true 
        };
        
        iframe.contentWindow.CancelOrderHandler.passData(data);
	},function(action){
		if(action == "save"){
			OrderHandler.orderGrid.unmask();
			OrderHandler.orderGrid.reload();
		}
	});
	
	miniWin.show();
}

/**
 * 批量移除订单处理程序
 * @param {} rowsData
 */
OrderHandler.yiChuOrders = function(rowsData){
	mini.confirm("是否移除选中的" + rowsData.length + "条订单资源？" ,"系统提示",
		function(action, value){
			if (action == "ok") {
				var ids = "";
				for ( var i = 0; i < rowsData.length - 1; i++){
					ids += rowsData[i].boiId + ",";
				}
				ids += rowsData[rowsData.length - 1].boiId;
				
				$.ajax({
			        url: OrderHandler.mgrData.yiChuOrdersUrl(),
			        type: "post",
			        dataType : 'json',
			        data: {
			    		boiIds: ids, 
			    		operateUType: gbOperateUType,
			    		operateUser: gbOperateUser,
			    		operateIp: gbOperateIp
			        },
			        cache: false,
			        success: function (text) {
			        	OrderHandler.orderGrid.reload();
			        },
			        error: function (jqXHR, textStatus, errorThrown) {
			        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
			        },
					complete : function(){
						OrderHandler.orderGrid.unmask();
					}
			    });
			    
			    OrderHandler.orderGrid.loading();
			}
		});
}

/**
 * 订单信息批量预处理程序
 * @param {} disposeType
 */
OrderHandler.disposeOrder = function(disposeType){
	var rowsData = OrderHandler.orderGrid.getSelecteds();
	
	if(rowsData.length == 0){
		MiniuiTools.miniWarning("系统提示","请选择要处理的资源！");
		return;
	}
	
	if(disposeType == OrderHandler.mgrData.queRBtn){
		if(OrderHandler.statinValidCounts(disposeType, rowsData)){
			OrderHandler.queRenOrders(rowsData);	
		}				
	}else if(disposeType == OrderHandler.mgrData.quXBtn){
		if(OrderHandler.statinValidCounts(disposeType, rowsData)){
			if(rowsData[0].boiPay == 0){       //付款前的批量取消操作
				OrderHandler.showCancelWin(rowsData,false);
			}else if(rowsData[0].boiPay == 1){ //付款后的批量取消操作
				OrderHandler.showCancelWin(rowsData, true);
			}
			
			return;
		}	
	}else if(disposeType == OrderHandler.mgrData.yiCBtn){
		if(OrderHandler.statinValidCounts(disposeType, rowsData)){
			OrderHandler.yiChuOrders(rowsData);
		}	
	}
}

/**
 * 展开、折叠查询按钮点击事件处理程序
 */
OrderHandler.orderQuery = function() {
	if (OrderHandler.isQueryBtnExpended) {
		OrderHandler.isQueryBtnExpended = false;
		mini.get("order_list_query").setText("展开查询");
		mini.get("order_list_query").setIconCls("icon-btn-expand");
	} else {
		OrderHandler.isQueryBtnExpended = true;
		mini.get("order_list_query").setText("收起查询");
		mini.get("order_list_query").setIconCls("icon-btn-collapse");
	}
	$("#queryOrderDiv").toggle(500);
	setTimeout(OrderHandler.doLayout, 600);
};

/**
 * 智能调整界面布局
 */
OrderHandler.doLayout = function(){
	OrderHandler.orderPanel.doLayout();
}

/**
 * 查询订单点击事件处理程序
 */
OrderHandler.queryOrderList = function() {
	var orderQueryData = mini.encode(OrderHandler.orderQueryForm.getData());
	OrderHandler.orderGrid.load({orderQueryData: orderQueryData});
}

/**
 * 刷新按钮点击事件处理程序
 */
OrderHandler.afresh = function() {
	OrderHandler.queryOrderList();
	OrderHandler.doLayout();
}

OrderHandler.toHigherQuery = function(){
	window.location.href = "orderQuery.jsp";
}

/**
 * 查看详情点击事件处理程序
 */
OrderHandler.findDetails = function(){
	if (!MiniuiTools.hasPurview(OrderHandler.purJsonOrder.orderDetail)) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var rowData = OrderHandler.orderGrid.getSelecteds();
	var boiId = rowData[0].boiId;
	var bmbId = rowData[0].boiMember;
	
	window.location.href="orderDetail.jsp?boiId="+boiId+"&bmbId="+bmbId;
}

/**
 * 表格绘画事件处理程序
 */
OrderHandler.orderGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var value = e.value;
     var innerHtml  = "";
     var indigoStr  = "<font style='color:#4B0082'>";
     var medblueStr = "<font style='color:#0000CD'>";
     var greenStr   = "<font style='color:#008000'>"; 
     var orgedStr   = "<font style='color:#FF0066'>";
     var redStr     = "<font style='color:#FF0000'>";
     var fontLas    = "</font>";
	
	switch (column.field) {
		case "boiOtime":
		{
			var bmbNick = (record.bmbNick == "") ? "<font color='#FF4A00'>匿名用户</font>" : record.bmbNick;
			if(value != null && value != "") value = XMAppUtils.changeDateToDateTimeStr(value, false);
			innerHtml = "<a href='javascript:void(0);'>" + bmbNick + "</a><p style='margin-top:4px;'>" + value + "</p>";
			break;
		}
		case "boiConsignee":
		{
			var boiMobile = record.boiMobile;
			var boiPhone = record.boiPhone;
			var boiDetail = record.boiDetail;
			var temphtml = "";
			var name = "<a href='javascript:void(0);' style='width:80px;display:inline-table;'>" + value + "</a>";
			var sep  = "<span style='border-left:dashed 1px #D2D2D2'>&nbsp;&nbsp;</span>";
			var mob  = "<span style='width:125px;display:inline-table;' class='icon-btn-phone'>&nbsp;&nbsp;&nbsp;:&nbsp;" + boiMobile + "</span>";
			var tel  = "<span style='width:100px;display:inline-table;'>" + boiPhone + "</span>";
			var hr   = "<hr style='width:100%;margin-top:1px;margin-bottom:1px;border:0;border-bottom:dashed 1px #D2D2D2;'/>";
			
			if(boiPhone != ""){temphtml = sep + mob + sep + tel;}else{temphtml = sep + mob;}
			innerHtml = name + temphtml + hr + boiDetail;
			break;
		}
		case "boiGpay":
		{
			innerHtml = "￥0.00元";
			if (value != ""){
				innerHtml = "￥" + parseFloat(value).toFixed(2) + "元";
			}
			break;
		}
		case "boiTpay":
		{
			innerHtml = "￥0.00元";
			if (value != ""){
				innerHtml = "￥" + parseFloat(value).toFixed(2) + "元";
			}
			break;
		}
		case "boiOrder":
		{
			innerHtml = OrderHandler.mgrData.boiOrderData[value + 1].text;
			
			if(value == 0){
				innerHtml = greenStr + innerHtml + fontLas;
			}else if(value == 1){
				innerHtml = indigoStr + innerHtml + fontLas;
			}else if(value == 2 || value == 4){
				innerHtml = redStr + innerHtml + fontLas;
			}else if(value == 3){
				innerHtml = medblueStr + innerHtml + fontLas;
			}
			break;
		}
		case "boiPay":
		{
			innerHtml = OrderHandler.mgrData.boiPayData[value + 1].text;
			
			if(value == 0){
				innerHtml = greenStr + innerHtml + fontLas;
			}else if(value == 1){
				innerHtml = medblueStr + innerHtml + fontLas;
			}else if(value == 2){
				innerHtml = redStr + innerHtml + fontLas;
			}
			break;
		}
		case "boiShip":
		{
			innerHtml = OrderHandler.mgrData.boiShipData[value + 1].text;
			
			if(value == 0){
				innerHtml = greenStr + innerHtml + fontLas;
			}else if(value == 1 || value == 2){
				innerHtml = indigoStr + innerHtml + fontLas;
			}else if(value == 3){
				innerHtml = orgedStr + innerHtml + fontLas;
			}else if(value == 4){
				innerHtml = medblueStr + innerHtml + fontLas;
			}else if(value == 5 || value == 6){
				innerHtml = redStr + innerHtml + fontLas;
			}
			break;
		}
		case "boiOperate":
		{
			innerHtml = "<span><a style='cursor: pointer;' onclick='OrderHandler.findDetails()'>详情</a></span>";
			break;
		}
	}
	
	if (innerHtml) e.cellHtml = innerHtml;
    
});


